package com.system.util.freemarker.template;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.component.domain.Note;
import com.system.config.freemarker.DirectiveHandler;
import com.system.config.freemarker.FreemarkerScan;
import com.system.config.freemarker.TemplateDirective;
import com.system.es.NoteEsServer;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@FreemarkerScan("estitlelist")
public class EsTitleListTemplate extends TemplateDirective {


    @Autowired
    private NoteEsServer noteEsServer;

    @Override
    public String getName() {
        return "estitlelist";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        Integer pageNum=handler.getInteger("pageNum");
        Integer pageSize=handler.getInteger("pageSize");
        String keyword=handler.getString("keyword");
        Map<String,Object> data=noteEsServer.page(keyword,pageNum,pageSize);
        System.out.println(JSONObject.toJSONString(data));
        Map<String,Object> map=new HashMap<>();
        map.put("total",data.get("total"));
        map.put("records",data.get("data")==null ? new ArrayList<>(): data.get("data"));
        handler.put(RESULT,map).render();
    }
}
